package com.example.mybatisplus.tx.service.impl;

import com.example.mybatisplus.des.User;
import com.example.mybatisplus.tx.dao.mapper.TxMapper;
import com.example.mybatisplus.tx.service.TxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

/**
 * @author zhouxl
 * @DATE 2021/5/16
 * @descriptoin
 */
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Component
public class TxServiceImpl implements TxService {

    @Autowired
    private TxMapper txMapper;

    @Override
    public Object addUser() {
        int randomNum = new Random().nextInt(1000);
        User user = new User(randomNum + "", randomNum + "", randomNum);
        int count = txMapper.addUser(user);
        return count != 0 ? "success" : "fail";
    }
}
